import React from 'react'
import {connect} from 'dva'
import {Table, Row, Col, Form, Input, Select, Button, Popconfirm} from 'antd';
import RightBox from "../../../components/commons/RightBox";
import RightContacts from "../../../utils/RightConstants";
import Constants  from '../../../utils/Constants';
import CurrencyDetail from '../../../components/basedata/currencyDetail';
const FormItem = Form.Item;

const currency = Form.create()(class Currency extends React.Component{
    constructor(props) {
        super(props);
    }

    componentDidMount() {
        this.props.dispatch({ type: 'currency/getCurrencyList', payload: { page: Constants.DEFAULT_PAGE } });
    }

    onEdit = (id) => {
        this.props.dispatch({
            type: 'currency/getCurrencyById',
            payload: {id}
        });
        this.currencyDetail.handleOpen();
    };
    
    handleSearch = (flag) => {
        let newPage = this.props.currency.page;
        if(flag) newPage.page = 1;
        this.props.dispatch({
            type: 'currency/getCurrencyList',
            payload: {
                ...this.props.form.getFieldsValue(),
                page: newPage
            }
        });
    };

    columns = [{
        title: '币种编码',
        dataIndex: 'currencyCode',
        key: 'currencyCode',
    }, {
        title: '币种名称',
        dataIndex: 'currencyName',
        key: 'currencyName',
    }, {
        title: '描述',
        dataIndex: 'currencyDesc',
        key: 'currencyDesc',
    }, {
        title: '创建时间',
        dataIndex: 'createtime',
        key: 'createtime',
    }, {
        title: '操作',
        render: (record) => {
            let editCurrencyButton=<RightBox resource={RightContacts.BASEDATA.BASE_CURRENCY_EDIT}>
                <a href="#" onClick={this.onEdit.bind(this, record.id)}>编辑</a>
                <span className="ant-divider"/>
            </RightBox>;

            let deleteCurrencyButton=<RightBox resource={RightContacts.BASEDATA.BASE_CURRENCY_DELETE}>
                <Popconfirm placement="leftTop" title={'数据删除后将不可以恢复，是否继续删除？'} onConfirm={() => {
                    this.props.dispatch({
                        type: 'currency/deleteCurrencyById',
                        payload: {
                            id : record.id,
                            onSuccess: () => this.handleSearch()
                        }
                    });
                }} okText="是" cancelText="否">
                    <a href="#">删除</a>
                </Popconfirm>
            </RightBox>;

            return (
                <span>
                    {editCurrencyButton}
                    {deleteCurrencyButton}
                </span>
            );
        }
    }];

    render(){
        let formItemLayout = { labelCol: {span: 8}, wrapperCol: {span: 16}};
        let values = this.props.form.getFieldsValue();
        const { getFieldDecorator } = this.props.form;

        let pagination = {
            showTotal: total => `共 ${total} 条数据`,
            pageSize: this.props.currency.page.size,
            current: this.props.currency.page.page,
            total: this.props.currency.page.totalRow,
            showSizeChanger: true,
            onShowSizeChange: (current, size) => {
                let newPage = this.props.currency.page;
                newPage.size = size;
                newPage.page = 1;
                this.props.dispatch({
                    type: 'currency/getCurrencyList',
                    payload: {
                       ...values,
                        page: newPage
                    }
                });
            },
            onChange: (pageNumber, pageSize) => {
                let newPage = this.props.currency.page;
                newPage.page = pageNumber;
                this.props.dispatch({
                    type: 'currency/getCurrencyList',
                    payload: {
                        ...values,
                        page: newPage
                    }
                });
            }
        }

        return (
            <div>
                <Row>
                    <Col span={6}>
                        <FormItem {...formItemLayout} label={'币种编码'}>
                            {getFieldDecorator('currencyCode')(
                                <Input />
                            )}
                        </FormItem>
                    </Col>
                    <Col span={6}>    
                        <FormItem {...formItemLayout} label={'币种名称'}>
                            {getFieldDecorator('currencyName')(
                                <Input />
                            )}
                        </FormItem>
                    </Col>    
                    <Col span={7} style={{ float: 'right' }}>
                        <Button type="primary" icon="search" style={{marginRight: 5}} onClick={this.handleSearch.bind(this, true)}>查询</Button>
                        <RightBox resource={RightContacts.BASEDATA.BASE_CURRENCY_ADD}><Button icon="plus" onClick={this.onEdit.bind(this, null)}>添加币种</Button></RightBox>
                    </Col>   
                </Row> 

                <Table rowKey="id" dataSource={this.props.currency.list} loading={this.props.loading} columns={this.columns} bordered={true} size={'middle'} scroll={{ x: true }} pagination={pagination} />
                <CurrencyDetail wrappedComponentRef={(inst) => this.currencyDetail = inst} refreshList={this.handleSearch} />
            </div>
        );
    }
})

function mapStateToProps(state){
    return{
        loading: state.loading.models.currency, currency: state.currency
    };
}

export default connect(mapStateToProps)(currency);
